package com.ego.controller;

import com.ego.pojo.EgoResult;
import com.ego.pojo.SearchPojo;
import com.ego.servcie.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class SearchController {

    @Autowired
    private SearchService searchService;

    @RequestMapping("/search.html")
    public String toSearch(String q,
                           @RequestParam(value = "page", defaultValue = "1") int page,
                           @RequestParam(value = "rows", defaultValue = "12") int rows,
                           Model model) {
        Map<String, Object> result = searchService.searchItem(q,page, rows);
        model.addAllAttributes(result);
        return "search";
    }

    @RequestMapping("/itemInsert")
    @ResponseBody
    public EgoResult itemInsert(@RequestBody SearchPojo searchPojo) {
       return searchService.itemInsert(searchPojo);
    }

    @RequestMapping("/itemDelete")
    @ResponseBody
    public EgoResult itemDelete(String[] ids) {
        return searchService.itemDelete(ids);
    }

    @RequestMapping("/itemReshelf")
    @ResponseBody
    public EgoResult itemReshelf(Long[] ids) {
        return searchService.itemReshelf(ids);
    }
}
